package com.synergy.stockflow.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.synergy.stockflow.annotation.Log;
import com.synergy.stockflow.annotation.LogModule;
import com.synergy.stockflow.entity.SysPermission;
import com.synergy.stockflow.entity.dto.SysPermDTO;
import com.synergy.stockflow.entity.vo.QuerySysPermissionVO;
import com.synergy.stockflow.model.Result;
import com.synergy.stockflow.service.SysPermissionService;
import com.synergy.stockflow.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "权限菜单", description = "管理权限和菜单的数据库操作")
@RestController
@RequestMapping("/permission")
@LogModule(name = "权限列表")
public class SysPermissionController {

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;


    @PostMapping("/tree")
    @Log(title = "查询树形权限菜单")
    @Cacheable(cacheNames = "permission", key = "'tree:'+ @sysUserServiceImpl.currentUser?.username")
    @Operation(method = "/permission/tree")
    public Result<List<SysPermission>> tree() {
        List<SysPermission> permissions = sysPermissionService.queryTree();
        return Result.success(permissions);
    }

    @PostMapping("/{id}")
    @Log(title = "根据编号查询权限")
    public Result<SysPermission> one(@PathVariable Integer id) {
        SysPermission sysPermission = sysPermissionService.getById(id);
        LambdaQueryWrapper<SysPermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysPermission::getParentId, id);
        sysPermission.setChildren(sysPermissionService.list(queryWrapper));
        return Result.success(sysPermission);
    }


    @PostMapping("/list")
    @Log(title = "根据名称或标题模糊查询权限菜单列表")
    @CachePut(cacheNames = "permission", key = "#queryVo.name")
    public synchronized Result<List<SysPermission>> list(@RequestBody QuerySysPermissionVO queryVo) {
        return Result.success(sysPermissionService.list(queryVo));
    }

    @PostMapping("/menu")
    @Log(title = "根据名称或标题模糊查询权限菜单列表")
    public synchronized Result<List<SysPermission>> menus(@RequestBody QuerySysPermissionVO queryVo) {
        return Result.success(sysPermissionService.listMenu(queryVo));
    }

    @PostMapping("/insert")
    @Log(title = "添加权限菜单")
    @CacheEvict(cacheNames = {"auth", "permission"}, allEntries = true)
    public Result<Boolean> insert(@RequestBody SysPermDTO dto) {
        Boolean b = sysPermissionService.insertPerm(dto);
        return Result.success(b);
    }


    @PutMapping("/update")
    @Log(title = "根据编号修改权限菜单")
    @CacheEvict(cacheNames = {"auth", "permission"}, allEntries = true)
    public Result<Boolean> update(@RequestBody SysPermDTO dto) {
        boolean updated = sysPermissionService.updatePerm(dto);
        return Result.success(updated, updated ? "修改成功！" : "修改失败！");
    }

    @DeleteMapping("/del/{id}")
    @Log(title = "删除权限菜单 - 物理删除")
    @CacheEvict(cacheNames = {"auth", "permission"}, allEntries = true)
    public Result<Boolean> remove(@PathVariable("id") Long id) {
        boolean removed = sysPermissionService.removeById(id);
        return Result.success(removed, removed ? "删除成功！" : "删除失败！");
    }
}
